package com.jinmdz.fmis.dao.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户表Provider
 *
 * @author LiCongLu
 * @date 2020-01-16 09:28
 */
public class SystemUserProvider {

    /**
     * 按照用户组编码查询员工信息
     *
     * @param codeList 用户组编码
     * @return
     * @author LiCongLu
     * @date 2020-01-16 09:38
     */
    public String listRoleUserViewByCodes(@Param("codeList") ArrayList<String> codeList) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" system_user.id,role_code,login_username,job_no,full_name,system_user.mobile_phone,system_user.other_phone");
                builder.append(" ,system_user.disable_state,as_login,ic_card_code,system_user.sort,system_user.version ");
                SELECT(builder.toString());
                FROM(" role_user_relation ");
                JOIN(" business_system_role on business_system_role.id = role_user_relation.business_system_role_id ");
                JOIN(" system_user on system_user.id = role_user_relation.system_user_id");
                WHERE(" business_system_role.deleted = 0 and system_user.deleted = 0");
                // 拼接用户组编码
                String codes = StringUtils.arrayToDelimitedString(codeList.toArray(new String[]{}), "','");
                WHERE("  business_system_role.role_code in ('" + codes + "')");
            }
        }.toString();
    }

    public String listSystemUserByIds(@Param("userIds") List<Integer> userIds) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append("id,login_username,login_password,login_secret_key,job_no,full_name,user_gender_code,mobile_phone,other_phone ,address,company_id,department_id,disable_state,as_login,ic_card_code,sort,remark,version");
                SELECT(builder.toString());
                FROM(" system_user ");
                // 拼接用户id
                String ids = StringUtils.arrayToDelimitedString(userIds.toArray(new Integer[]{}), "','");
                WHERE("  system_user.id in ('" + ids + "')");

            }

        }.toString();


    }
}
